package com.org.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.org.mapper.OrderrecordMapper;
import com.org.model.Conferenceroom;
import com.org.model.Orderrecord;
import com.org.model.User;
import com.org.service.impl.ConferenceroomServiceImpl;
import com.org.service.impl.OrderrecordServiceImpl;
import com.org.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author test
 * @since 2021-06-05
 */
@CrossOrigin
@RestController
@RequestMapping("/orderrecord")
public class OrderrecordController {

    @Autowired
    private OrderrecordMapper orderrecordMapper;
    @Autowired
    private OrderrecordServiceImpl orderrecordService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private ConferenceroomServiceImpl roomService;

    @PostMapping("/getOrderList")
    public Map<String,Object> selectAllRecord(@RequestParam(name = "current") Integer current,
                                              @RequestParam(name = "size") Integer size){
        Map<String,Object> data = new HashMap<>();
        Page<Orderrecord> page = new Page<>(current,size);
        IPage<Orderrecord> list = orderrecordService.selectAllPage(page);
        data.put("code",200);
        data.put("msg","success");
        data.put("data",list);
        return data;
    }

    @PostMapping("/getNowPage")
    public Map<String,Object> selectNowPage(@RequestParam(name = "current") Integer current, @RequestParam(name = "size") Integer size,
                                            @RequestParam(name = "uid") Integer uid){
        Page<Orderrecord> page = new Page<>(current, size);
        LocalDateTime time = LocalDateTime.now();
        IPage<Orderrecord> RecordsPage = orderrecordMapper.selectOrdPage(page,uid);
        Map<String,Object> data= new HashMap<>();
        data.put("code",200);
        data.put("msg","查询成功");
        data.put("data",RecordsPage);
        return data;
    }

    @PostMapping("/SignIn")
    public Map<String,Object> SignIn(Orderrecord orderrecord){
        Map<String,Object> data = new HashMap<>();
        data.put("code",200);
        LocalDateTime startTime = orderrecord.getStartTime();
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(startTime,now);
        long time = duration.toMinutes();
        System.out.println(time);

        if (time<30 && time >0)
        {
            orderrecord.setStatus("已签到");
            boolean check = orderrecordService.updateById(orderrecord);
            if (check){
                data.put("msg","success");
            }
        }else if (time<0){
            data.put("msg","未到时间");
        }else  {
            data.put("msg","超过预约时间，签到失败");
        }
        return data;
    }

    @PostMapping("/getRecordsPage")
    public Map<String,Object> selectPage(@RequestParam(name = "current") Integer current, @RequestParam(name = "size") Integer size,
                                         @RequestParam(name = "uid") Integer uid){
        Page<Orderrecord> page = new Page<>(current, size);
        LocalDateTime time = LocalDateTime.now();
        IPage<Orderrecord> RecordsPage = orderrecordMapper.selectNowPage(page,uid,time);
        Map<String,Object> data= new HashMap<>();
        data.put("code",200);
        data.put("msg","查询成功");
        data.put("data",RecordsPage);
        return data;
    }

    @PostMapping("/change")
    public Map<String,Object> change(){
        Map<String,Object> data = new HashMap<>();
        data.put("code",200);
        return data;
    }

    @PostMapping("/remove")
    public Map<String,Object> remove(Orderrecord orderrecord){
        Map<String,Object> data = new HashMap<>();
        data.put("code",200);
        LocalDateTime startTime = orderrecord.getStartTime();
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(startTime,now);
        long time = duration.toMinutes();
        if (time<0){
            boolean check = orderrecordService.removeById(orderrecord);
            if (check){
                data.put("msg","取消成功");
            }else {
                data.put("msg","取消失败");
            }
        }else {
            data.put("msg","超过取消时间");
        }
        return data;
    }

    @PostMapping("/update")
    public Map<String,Object> updateById(Orderrecord orderrecord){
        Map<String,Object> data = new HashMap<>();
        boolean check =  orderrecordService.updateById(orderrecord);
        data.put("code",200);
        if (check ){
            data.put("msg","更新成功");
        }else {
            data.put("msg","更新失败");
        }
        return data;
    }

    @DeleteMapping("/deleteById")
    public Map<String,Object> deleteById(@RequestParam String id){
        Map<String,Object> data = new HashMap<>();
        boolean check = orderrecordService.removeById(id);
        // boolean check = serviceImp.deleteById(id);
        if (check){
            data.put("code",200);
            data.put("msg","删除成功");
            return data;
        }
        data.put("code",400);
        data.put("msg","删除失败");
        return data;
    }

    @PostMapping("/getBfRecordsPage")
    public Map<String,Object> selectBfPage(@RequestParam(name = "current") Integer current, @RequestParam(name = "size") Integer size,
                                         @RequestParam(name = "uid") Integer uid){
        Page<Orderrecord> page = new Page<>(current, size);
        IPage<Orderrecord> RecordsPage = orderrecordMapper.selectPageByUid(page,uid);
        Map<String,Object> data= new HashMap<>();
        data.put("code",200);
        data.put("msg","查询成功");
        data.put("data",RecordsPage);
        return data;
    }

    @PostMapping("/getDfRecordsPage")
    public Map<String,Object> selectDfPage(@RequestParam(name = "current") Integer current, @RequestParam(name = "size") Integer size,
                                           @RequestParam(name = "uid") Integer uid){
        Page<Orderrecord> page = new Page<>(current, size);
        LocalDateTime time = LocalDateTime.now();
        IPage<Orderrecord> RecordsPage = orderrecordMapper.selectDefaultPage(page,uid,time);
        Map<String,Object> data= new HashMap<>();
        data.put("code",200);
        data.put("msg","查询成功");
        data.put("data",RecordsPage);
        return data;
    }

    @PostMapping("checkRecords")
    public Map<String,Object> checkRecords(@RequestParam(name = "count") Integer count,
                                           @RequestParam(name = "startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                                           @RequestParam(name = "endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
                                           @RequestParam(name = "uId") Integer uid
                                           ){
        Map<String,Object> data = new HashMap<>();
        List<Conferenceroom> rooms = roomService.getRoomByCount(count);
        List<Orderrecord> records;
        Orderrecord orderrecord = new Orderrecord();
        LocalDateTime time = LocalDateTime.now();
        int check = 0;
        if (rooms!=null) {
            for (Conferenceroom room : rooms) {
                records = orderrecordService.selectByCid(room.getCId());
                if (records!=null) {
                    for (Orderrecord record : records) {
                        if (startTime.isEqual(record.getStartTime())
                                && endTime.isEqual(record.getEndTime())){
                                data.put("code",400);
                                data.put("msg","所选时间重复");
                                return data;
                        }else {
                            check++;
                        }
                    }
                    if (check>records.size()){
                        orderrecord.setCId(room.getCId());
                        orderrecord.setCreateTime(time);
                        orderrecord.setStartTime(startTime);
                        orderrecord.setEndTime(endTime);
                        orderrecord.setPeopleCount(count);
                        orderrecord.setStatus("未签到");
                        orderrecord.setUId(uid);
                        boolean isUpdate = orderrecordService.save(orderrecord);
                        data.put("code",200);
                        data.put("msg","当前时间段有可预约房间");
                        data.put("roomList",room);
                        if (isUpdate){
                            data.put("type","成功预约！");
                        }else {
                            data.put("type","预约失败！");
                        }
                        return data;
                    }
                }
            }
            data.put("code",400);
            data.put("msg","暂无满足条件的房间");
        }
        return data;
    }
}
